/**
 * 
 */
package com.itinsys.enma.util.impl;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

import org.hibernate.lob.SerializableBlob;
import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.BuildingDAO;
import com.itinsys.enma.dao.EnvironmentDAO;
import com.itinsys.enma.dao.ResourceDAO;
import com.itinsys.enma.dao.RoleDAO;
import com.itinsys.enma.dao.UserDAO;
import com.itinsys.enma.domain.Building;
import com.itinsys.enma.domain.Environment;
import com.itinsys.enma.domain.Resource;
import com.itinsys.enma.domain.User;
import com.itinsys.enma.util.InitializationService;
import com.itinsys.enma.util.Utils;
import com.thoughtworks.xstream.XStream;

/**
 * @author Pablo
 * 
 */
public class InitializationServiceImpl implements InitializationService {

	@Autowired
	RoleDAO roleDAO;
	@Autowired
	ResourceDAO resourceDAO;
	@Autowired
	EnvironmentDAO environmentDAO;
	@Autowired
	UserDAO userDAO;
	@Autowired
	BuildingDAO buildingDAO;
	
	File file;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.itinsys.enma.util.InitializationService#initialize()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean initialize() {
		XStream xstream=new XStream();
		try {
			List<Map<String,Object>> datas=(List<Map<String, Object>>) xstream.fromXML(Utils.readContentFromFile(file.getPath()));
			for(Map<String,Object> data:datas)
				for(String key:data.keySet()){
					if(key.equalsIgnoreCase("resources")){
						List<Resource> resources=(List<Resource>) data.get(key);
						for(Resource r:resources)
							resourceDAO.addResource(r);
					}else if(key.equalsIgnoreCase("users")){
						List<User> users=(List<User>) data.get(key);
						for(User u:users)
							userDAO.add(u);
					}else if(key.equalsIgnoreCase("buildings")){
						List<Building> buildings=(List<Building>) data.get(key);
						for(Building b:buildings)
							buildingDAO.add(b);
					}
					else if(key.equalsIgnoreCase("environments")){
						List<Environment> environments=(List<Environment>) data.get(key);
						for(Environment e:environments)
							insertEnvironment(e);
					}
				}
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	
	
	private void insertEnvironment(Environment e) {
		Set<Resource> tempResources=new HashSet<Resource>();
		tempResources.addAll(e.getResources());
		byte[] byteArray = ByteBuffer.allocate(4).putInt(1695609641).array();;
		//Inserto un valor por defecto a location que no es null
		try {
			e.setLocation(new SerializableBlob(new SerialBlob(byteArray)));
		} catch (SerialException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		e.setResources(new HashSet<Resource>());
		
		for(Resource r:tempResources){
			e.getResources().add(resourceDAO.getById(r.getId()));
		}
		environmentDAO.addEnvironment(e);
	}



	public InitializationServiceImpl(File file) {
		this.file=file;
	}


}
